1 module hip.asset_manager.load_task; 2 import hip.concurrency.thread; 3 import hip.config.opts; 4 import hip.api.data.commons; 5 import hip.error.handler; 6 import hip.assetmanager; 7 8 9 class HipAssetLoadTask : IHipAssetLoadTask 10 { 11 string name; 12 string path; 13 HipAssetResult _result = HipAssetResult.cantLoad; 14 HipAsset _asset = null; 15 HipWorkerThread worker; 16 string error; 17 private string fileRequesting; 18 private size_t lineRequesting; 19 20 this(string path, string name, HipAsset asset, const(ubyte)[] extraData, string fileRequesting, size_t lineRequesting) 21 { 22 assert(name != null, "Asset load task can't receive null name"); 23 this.path = path; 24 this.name = name; 25 this._asset = asset; 26 this.fileRequesting = fileRequesting; 27 this.lineRequesting = lineRequesting; 28 if(asset is null) 29 _result = HipAssetResult.waiting; 30 else 31 _result = HipAssetResult.loaded; 32 } 33 34 bool hasFinishedLoading() const{return result == HipAssetResult.loaded;} 35 bool opCast(T : bool)() const{return hasFinishedLoading;} 36 37 38 void addOnCompleteHandler(void delegate(HipAsset) onComplete) 39 { 40 HipAssetManager.addOnCompleteHandler(this, onComplete); 41 } 42 void addOnCompleteHandler(void delegate(string) onComplete) 43 { 44 HipAssetManager.addOnCompleteHandler(this, (asset) 45 { 46 HipFileAsset theAsset = cast(HipFileAsset)asset; 47 assert(theAsset !is null, "Asset received is not a text"); 48 onComplete(theAsset.getText); 49 }); 50 } 51 52 53 void into(string*[] variables...) 54 { 55 import hip.error.handler; 56 final switch(_result) with(HipAssetResult) 57 { 58 case waiting, mainThreadLoading, loading: 59 //variables are implicitly `scope`, need to duplicate. 60 string*[] vars = variables.dup; 61 addOnCompleteHandler((string data) 62 { 63 foreach(v; vars) 64 *v = data; 65 }); 66 break; 67 case loaded: 68 foreach(v; variables) 69 *v = (cast(HipFileAsset)(asset)).getText; 70 break; 71 case cantLoad: 72 ErrorHandler.showWarningMessage("Can't load a null asset into a variable address", name); 73 break; 74 } 75 } 76 77 78 void into(void* function(HipAsset asset) castFunc, HipAsset*[] variables...) 79 { 80 import hip.error.handler; 81 final switch(_result) with(HipAssetResult) 82 { 83 case waiting, mainThreadLoading, loading: 84 //variables are implicitly `scope`, need to duplicate. 85 HipAsset*[] vars = variables.dup; 86 addOnCompleteHandler((HipAsset completeAsset) 87 { 88 HipAsset theAsset = cast(HipAsset)castFunc(completeAsset); 89 assert(theAsset !is null, "Null asset received in complete handler?"); 90 foreach(v; vars) 91 *v = theAsset; 92 }); 93 break; 94 case loaded: 95 foreach(v; variables) 96 *v = cast(HipAsset)castFunc(asset); 97 break; 98 case cantLoad: 99 ErrorHandler.showWarningMessage("Can't load a null asset into a variable address", name); 100 break; 101 } 102 } 103 104 void await() 105 { 106 if(_result == HipAssetResult.loading) 107 HipAssetManager.awaitTask(this); 108 } 109 void update(){} 110 111 HipAssetResult result() const {return _result;} 112 HipAsset asset(){return _asset;} 113 HipAssetResult result(HipAssetResult newResult){return _result = newResult;} 114 HipAsset asset(HipAsset newAsset){return _asset = cast(HipAsset)newAsset;} 115 }